package com.kuang.demo.service.wx;

import com.github.pagehelper.PageHelper;
import com.kuang.demo.bean.wx.CouponExechangeStore;
import com.kuang.demo.bean.wx.CouponExechangeUser;
import com.kuang.demo.bean.wx.CouponSelectlist;
import com.kuang.demo.bean.wx.WXCoupon;
import com.kuang.demo.mapper.acg.UserMapper;
import com.kuang.demo.mapper.wx.WXCouponMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class WXCouponServiceImpl implements WXCouponService {
    @Autowired
    WXCouponMapper wxCouponMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WXCoupon queryCouponList(int status, int page, int size, int userId) {
        WXCoupon wxCoupon = new WXCoupon();
        int count = wxCouponMapper.selectCount(userId);
        wxCoupon.setCount(count);
        PageHelper.startPage(page, size);
        List<WXCoupon.DataBean> dataBeans = new ArrayList<>();
        List<Integer> coupinIds = wxCouponMapper.selectCoupIdByUserId(status);
        if (coupinIds != null) {
            for (Integer coupinId : coupinIds) {
                dataBeans.add(wxCouponMapper.selectById(coupinId));
            }
            wxCoupon.setData(dataBeans);
        }
        return wxCoupon;
    }

    @Override
    public WXCoupon queryCouponAllList(int page, int size) {
        WXCoupon wxCoupon = new WXCoupon();
        int count = wxCouponMapper.selectAllCount();
        wxCoupon.setCount(count);
        PageHelper.startPage(page, size);
        List<WXCoupon.DataBean> dataBeans = wxCouponMapper.selectAll();
        wxCoupon.setData(dataBeans);
        return wxCoupon;
    }

    /**
     * TODO
     * 需要添加数量判断
     * 数量-1
     * 能否领取
     * 添加时的时间判断输入
     *
     * @param couponId
     */
    @Override
    public void receiveCoupon(Integer couponId) {
        int userId = getId();
        WXCoupon.DataBean dataBean = wxCouponMapper.selectOneById(couponId);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = dateFormat.format(new Date());
        wxCouponMapper.insertCoupon(time, userId, dataBean);
    }

    /**
     * 各种规则
     *
     * @param cartId
     * @return TODO
     */
    @Override
    public CouponSelectlist selectlist(int cartId) {
        CouponSelectlist couponSelectlist = new CouponSelectlist();
        couponSelectlist.setCount(wxCouponMapper.selectCountListByStatus(getId()));
        List<Integer> usersCoupon = wxCouponMapper.selectCountList(getId());
        List<CouponSelectlist.DataBean> dataBeans = null;
        for (Integer integer : usersCoupon) {
            dataBeans = wxCouponMapper.selectCouponList(integer);
        }
        couponSelectlist.setData(dataBeans);
        return null;
    }

    /**
     * TODO
     * add time时间类没做
     *
     * @param code
     * @return
     */
    @Override
    public int couponExchange(String code) {
        CouponExechangeStore couponExechange = wxCouponMapper.selectCouponIdByCode(code);
        int i = wxCouponMapper.selectBoolHave(couponExechange.getId());
        if (i < couponExechange.getLimit() || couponExechange.getLimit() == 0) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            String time = dateFormat.format(new Date());
//            Date endTime=new Date();
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
//            String str="20110823";
//            Date dt=sdf.parse(str);
//            Calendar rightNow = Calendar.getInstance();
//            rightNow.setTime(dt);
//            rightNow.add(Calendar.YEAR,-1);//日期减1年
//            rightNow.add(Calendar.MONTH,3);//日期加3个月
//            rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
            Calendar time = Calendar.getInstance();
            String rightNow = dateFormat.format(time.getTime());
            time.add(Calendar.DAY_OF_YEAR, couponExechange.getTimeType());
            String endTime = dateFormat.format(time.getTime());
            int i1 = wxCouponMapper.addCouponToUser(getId(), couponExechange.getId(), rightNow, endTime);
            return i1;
        }
        return 0;
    }

    private int getId() {
        Subject subject = SecurityUtils.getSubject();
        String userName = (String) subject.getPrincipal();
        return userMapper.selectUserId(userName);
    }

}
